import { Base } from "./base";

export class Cate extends Base {
  /**
   *
   * @param name
   * @param module
   * @param pageNum
   * @param pageSize
   * @returns {Promise<void>}
   */
  // eslint-disable-next-line camelcase
  async list({ fid, module, pageNum = 1, pageSize = 10 }) {
    const { data = {} } = await this.$http("post", "/api/cate", {
      fid,
      module,
      pageNum,
      pageSize
    });
    data.data &&
      data.data.map(r => {
        r.has_children = r.has_children > 0;
      });
    return data;
  }

  /**
   *
   * @param id
   * @returns {Promise<boolean>}
   */
  async del(id) {
    const { errno } = await this.$http("post", "/api/cate/del", { id });
    return errno === 0;
  }

  /**
   *
   * @param name
   * @param module
   * @param title
   * @param fid
   * @param sort
   * @returns {Promise<void>}
   */
  async add({
    name,
    fid,
    title,
    module,
    sort,
    url,
    thumb,
    class_name,
    style,
    abstract,
    seo_title
  }) {
    const { errno } = await this.$http("post", "/api/cate/add", {
      name,
      module,
      title,
      fid,
      sort,
      url,
      thumb,
      class_name,
      style,
      abstract,
      seo_title
    });
    return errno === 0;
  }

  /**
   *
   * @param id
   * @param name
   * @param fname
   * @param title
   * @param sort
   * @param has_children
   * @returns {Promise<void>}
   */
  // eslint-disable-next-line camelcase
  async update({
    id,
    name,
    fname,
    title,
    sort,
    has_children,
    url,
    thumb,
    class_name,
    style,
    abstract,
    seo_title
  }) {
    const { errno } = await this.$http("post", "/api/cate/update", {
      id,
      name,
      fname,
      title,
      sort,
      url,
      has_children,
      thumb,
      class_name,
      style,
      abstract,
      seo_title
    });
    return errno === 0;
  }

  /**
   *
   * @param id
   * @param type
   * @returns {Promise<void>}
   */
  async detail(id, type = "detail", url, cache) {
    const { data } = await this.$http("post", "/api/cate/detail", {
      id,
      url,
      type,
      cache
    });
    return data;
  }

  /**
   *
   * @param cate_id
   * @param result
   * @returns {Promise<Array|*>}
   */
  async breadcrumbs(cate_id, result = []) {
    const data = await this.detail(cate_id);
    if (data && data.id) result.unshift(data);
    if (data && data.fid) return this.breadcrumbs(data.fid, result);
    else return result;
  }
}
